package com.ruoyi.project.datum.specification.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.datum.series.domain.Series;
import com.ruoyi.project.datum.specification.domain.Specification;
import com.ruoyi.project.datum.specification.service.ISpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：产品规格信息控制层
 * 时间：2020/5/18
 * 作者：WangHaiDong
 */
@Controller
@RequestMapping("/datum/specification")
public class SpecificationController extends BaseController {

    private String prefix = "datum/specification";

    @Autowired
    private ISpecificationService specificationService;

    /**
     * 产品规格列表界面
     * @return
     */
    @GetMapping()
    public String datum()
    {
        return  prefix+ "/specification";
    }

    /**
     * 条件查询所有的规格信息
     * @param specification
     * @return
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Specification specification){
        // BaseController 提供的分页
        startPage();
        List<Specification> list = specificationService.selectAll(specification);
        return getDataTable(list);
    }

    /**
     * 新增产品规格信息
     * @return
     */
    @GetMapping("/add")
    public String add(){

        return prefix + "/add";
    }

    /**
     * 新增产品系列 保存到数据库中
     * @param specification
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Specification specification)
    {
        if (UserConstants.SPE_NAME_NOT_UNIQUE.equals(specificationService.checkSpeNameUnique(specification)))
        {
            return error("新增产品规格'" + specification.getSpeName() + "'失败，产品规格称已存在");
        }
        return toAjax(specificationService.insertSpecification(specification));

    }

    /**
     * 校验产品规格名称
     */
    @PostMapping("/checkSpeNameUnique")
    @ResponseBody
    public String checkSpeNameUnique(Specification specification)
    {
        return specificationService.checkSpeNameUnique(specification);
    }

    /**
     * 根据产品规格ID 删除产品信息
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(specificationService.deleteSpecificationByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 修改产品规格信息
     */
    @GetMapping("/edit/{speId}")
    public String edit(@PathVariable("speId") Long speId, ModelMap mmap)
    {
        mmap.put("specification", specificationService.selectSpecificationById(speId));
        return prefix + "/edit";
    }

    /**
     * 修改保存 产品规格信息
     * @param specification
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Specification specification)
    {
        if (UserConstants.SPE_NAME_NOT_UNIQUE.equals(specificationService.checkSpeNameUnique(specification)))
        {
            return error("修改产品规格'" + specification.getSpeName() + "'失败，产品规格名称已存在");
        }
        return toAjax(specificationService.updateSpecification(specification));
    }

    /**
     * 查看产品规格信息
     */
    @GetMapping("/detail/{speId}")
    public String seriesView(@PathVariable("speId") Long speId, ModelMap mmap)
    {
        mmap.put("specification", specificationService.selectSpecificationById(speId));
        return prefix + "/detail";
    }

}
